package com.xauto.pioneer.appuser.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.xauto.pioneer.appuser.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lsx on 2017-11-22.
 */

public class ViewUtil {
    //设置按钮padding
    static int TextViewleft = 0;
    static int TextViewtop =0;
    static int TextViewright = 0;
    static int TextViewbottom = 0;
    static LinearLayout.LayoutParams params;
    public static void GetViewShgape(View v){
        TextViewleft = v.getPaddingLeft();
        TextViewtop = v.getPaddingTop();
        TextViewright = v.getPaddingRight();
        TextViewbottom = v.getPaddingBottom();
    }
    public static LinearLayout.LayoutParams GetViewSize(View v){
        params=(LinearLayout.LayoutParams)v.getLayoutParams();
    return params;
    }
    public static void SetViewSize(View v){
       v.setLayoutParams(params);
    }
    public static void SetViewShgape(View v){
        v.setPadding(TextViewleft, TextViewtop, TextViewright, TextViewbottom);
}

    /**缩略图放大成dialog显示*/
    public static void showBigPic(Context context,String path){
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
//        View imgEntryView = View.inflate(this,R.layout.dialog_openphoto, null); // 加载自定义的布局文件
//        dialog.setView(imgEntryView);
//        ImageView img1 = (ImageView)imgEntryView.findViewById(R.id.img_dialog_bigphoto);
//        Log.e("tag","path = "+path);
//        Bitmap bmp=BitmapFactory.decodeFile(path);
//        img1.setImageBitmap(bmp);
//        dialog.show();
//        imgEntryView.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View paramView) {
//                dialog.cancel();
//            }
//        });
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.show();
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setGravity(Gravity.CENTER);
        window.setContentView(R.layout.dialog_openphoto);
        ImageView img1 = (ImageView)window.findViewById(R.id.img_dialog_bigphoto);
        Bitmap bmp= BitmapFactory.decodeFile(path);
        img1.setImageBitmap(bmp);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.FILL_PARENT;
        window.setAttributes(lp);
        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
    /**
     * 检查手机上是否安装了指定的软件
     * @param context
     * @param packageName：应用包名
     * @return
     */
    public static boolean isAvilible(Context context, String packageName){
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if(packageInfos != null){
            for(int i = 0; i < packageInfos.size(); i++){
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }



}
